// 在 setup 外使用 useDialog、useMessage、useNotification、useLoadingBar，useModal，可以通过 createDiscreteApi 来构建对应的API
import { createDiscreteApi } from "naive-ui"

// 创建加载条
const bar = ref();

// 自定义插件
export default defineNuxtPlugin((nuxtApp) => {
    nuxtApp.hook("app:mounted", e => {
        if(!bar.value){
            const { loadingBar }  = createDiscreteApi(["loadingBar"])
            bar.value = loadingBar
        }
    });

    nuxtApp.hook("page:start", e => {
       bar.value?.start();
    })

    nuxtApp.hook("page:finish", e => {
        setTimeout(() => {
            bar.value?.finish();
        }, 150)
    });

    nuxtApp.hook("app:error", e => {
       if(process.client){
        setTimeout(() => {
            bar.value?.finish();
        }, 150)
       }
    });
});
